#!/bin/bash
#Creat IP Address Map
yum install -y expect
touch /tmp/hosts
echo "172.16.0.10 $3-00">>/tmp/hosts
for ((i=1; i<=$2; i++))
do
echo "172.16.0.1$i $3-0$i">>/tmp/hosts
done 
/usr/bin/expect << EOF
spawn cp /tmp/hosts /etc/hosts
expect "cp: overwrite ‘/etc/hosts’? "
send "y\r"
expect eof
exit
EOF
#Setting Password-Free Login
/usr/bin/expect << EOF
spawn ssh-keygen -t rsa -P ""
expect "(/root/.ssh/id_rsa): "
send "\r"
expect eof
exit
EOF
for ((i=0; i<=$2; i++))
do
/usr/bin/expect << EOF
spawn ssh-copy-id 172.16.0.1$i
expect "(yes/no)?"
send "yes\r"
expect "password:"
send "$1\r"
expect eof
exit
EOF
done
for ((i=1; i<=$2; i++))
do
scp ~/.ssh/* root@172.16.0.1$i:~/.ssh
scp /etc/hosts root@172.16.0.1$i:/etc/hosts
done
#Upate yum
mkdir /etc/yum.repos.d/repo_bak
mv /etc/yum.repos.d/*.repo /etc/yum.repos.d/repo_bak/
curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.myhuaweicloud.com/repo/CentOS-Base-7.repo
yum clean all
yum makecache
#Connect SFS
yum -y install nfs-utils -y
echo "$4 /hpc nfs vers=3,timeo=600,nolock 0 0" >> /etc/fstab
mkdir /hpc
mount -a
#Install munge
cd /hpc || exit
wget https://documentation-samples.obs.cn-north-4.myhuaweicloud.com/solution-as-code-publicbucket/solution-as-code-moudle/build-gene-sequencing-environment/open-source-software/hpcdata.tar.gz
tar -zxvf hpcdata.tar.gz
cd /hpc/hpcdata/munge/mungerpm || exit
yum install -y rpm-build rpmdevtools bzip2-devel openssl-devel zlib-devel readline-devel pam-devel perl-DBI perl-ExtUtils-MakeMaker mariadb*
yum install munge-* -y
chmod -Rf 700 /etc/munge
chmod -Rf 711 /var/lib/munge
chmod -Rf 700 /var/log/munge
chmod -Rf 0755 /var/run/munge
chown munge.munge /etc/munge/munge.key
sed -i "s/manage_etc_hosts: localhost/manage_etc_hosts: false/" /etc/cloud/cloud.cfg
systemctl start munge
systemctl enable munge
#Install slurm
cd /hpc/hpcdata/slurm/slurmrpm || exit
yum install -y slurm*
groupadd -g 202 slurm
useradd -u 202 -g 202 slurm
mkdir -p /var/spool/slurm/ssl
mkdir -p /var/spool/slurm/d
mkdir -p /var/log/slurm
chown -R slurm.slurm /var/spool/slurm
cp /etc/slurm/slurm.conf.example /etc/slurm/slurm.conf
ssh -t root@172.16.0.11> /dev/null 2>&1 <<eeooff
free -m > /tmp/memory.txt
lscpu > /tmp/cpus.txt
scp /tmp/memory.txt root@172.16.0.10:/tmp/memory.txt
scp /tmp/cpus.txt root@172.16.0.10:/tmp/cpus.txt
exit
eeooff
rmemory=$(sed -n "2p" /tmp/memory.txt | awk '{print $4}')
cpus=$(sed -n "4p" /tmp/cpus.txt | awk '{print $2}' |tr -d '\n')
soc=$(sed -n "8p" /tmp/cpus.txt | awk '{print $2}' |tr -d '\n')
corps=$(sed -n "7p" /tmp/cpus.txt | awk '{print $4}' |tr -d '\n')
thrpc=$(sed -n "6p" /tmp/cpus.txt | awk '{print $4}' |tr -d '\n')
sed -i "s/ClusterName=linux/ClusterName=slurm/" /etc/slurm/slurm.conf
sed -i "s/ControlMachine=linux0/ControlMachine=$3-00/" /etc/slurm/slurm.conf
sed -i "s/#ControlAddr=/#ControlAddr=172.16.0.10/" /etc/slurm/slurm.conf
sed -i "s/ReturnToService=0/ReturnToService=$2/" /etc/slurm/slurm.conf
sed -i "s/SlurmctldDebug=info/SlurmctldDebug=$(expr $2 + 1)/" /etc/slurm/slurm.conf
sed -i "s/SlurmdDebug=info/SlurmdDebug=$(expr $2 + 1)/" /etc/slurm/slurm.conf
sed -i "92d" /etc/slurm/slurm.conf
for ((i=1; i<=$2; i++))
do 
echo "NodeName=$3-0$i Realmemory=$(expr $rmemory \* 4 / 5) CPUs=$cpus Sockets=$soc CoresPerSocket=$corps ThreadsPerCore=$thrpc">>/etc/slurm/slurm.conf
done
systemctl start slurmctld
systemctl enable slurmctld
#Setting node
for ((i=1; i<=$2; i++))
do
ssh -t root@172.16.0.1$i> /dev/null 2>&1 <<eeooff
mkdir /etc/yum.repos.d/repo_bak
mv /etc/yum.repos.d/*.repo /etc/yum.repos.d/repo_bak/
curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.myhuaweicloud.com/repo/CentOS-Base-7.repo
yum clean all
yum makecache
yum -y install nfs-utils 
echo "$4 /hpc nfs vers=3,timeo=600,nolock 0 0" >> /etc/fstab
mkdir /hpc
mount -a
cd /hpc/hpcdata/munge/mungerpm || exit
yum install -y rpm-build rpmdevtools bzip2-devel openssl-devel zlib-devel readline-devel pam-devel perl-DBI perl-ExtUtils-MakeMaker mariadb*
yum install munge-* -y
scp root@172.16.0.10:/etc/munge/munge.key /etc/munge/munge.key
chmod -Rf 700 /etc/munge
chmod -Rf 711 /var/lib/munge
chmod -Rf 700 /var/log/munge
chmod -Rf 0755 /var/run/munge
chown munge.munge /etc/munge/munge.key
sed -i "s/manage_etc_hosts: localhost/manage_etc_hosts: false/" /etc/cloud/cloud.cfg
systemctl start munge
systemctl enable munge
cd /hpc/hpcdata/slurm/slurmrpm || exit
yum install -y slurm*
groupadd -g 202 slurm
useradd -u 202 -g 202 slurm
mkdir -p /var/spool/slurm/ssl
mkdir -p /var/spool/slurm/d
mkdir -p /var/log/slurm
chown -R slurm.slurm /var/spool/slurm
scp root@172.16.0.10:/etc/slurm/slurm.conf /etc/slurm/slurm.conf
systemctl start slurmd
systemctl enable slurmd
exit
eeooff
done

